package com.food.restaurantpro.database.tables;

import java.util.ArrayList;
import java.util.List;

import android.content.ContentResolver;
import android.content.ContentValues;
import android.database.Cursor;
import android.util.Log;

import com.food.restaurantpro.RestaurantApp;
import com.food.restaurantpro.database.RestaurantAppContentProvider;
import com.food.restaurantpro.objects.DishCategory;
import com.food.restaurantpro.objects.Promotion;

public class DishCategoryTable {
	public static final String TAG = "DishCategoryTable";
	
	public static final String TABLE_NAME = "dishcategory";
	
	public static final String DISH_CATEGORY_ID = "dishCategoryId";
	public static final String DISH_CATEGORY_NAME = "dishCategoryName";
	public static final String DISH_CATEGORY_IMAGE = "dishCategoryImage";
	
	public static final String SQL_EXCUTE = "CREATE TABLE " + TABLE_NAME + "("
			+ DISH_CATEGORY_ID + " text primary key not null, "
			+ DISH_CATEGORY_NAME + " text not null, "
			+ DISH_CATEGORY_IMAGE + " text not null);";
	
	public static void deleteAll()
	{
		ContentResolver resolver = RestaurantApp.CURRENT_CONTEXT.getContentResolver();
		resolver.delete(RestaurantAppContentProvider.DISH_CATEGORY_CONTENT_URI, null, null);
	}
	
	/**
	 * @param listDishCategory list dish_category to insert to db
	 * @return the number of newly created rows. 
	 */
	public static int insertListDishCategory(List<DishCategory> listDishCategory) {
		if(listDishCategory != null && listDishCategory.size() > 0) {
			int size = listDishCategory.size();
			//show progress
			ContentValues[] allValues = new ContentValues[size];
			for (int i = 0; i < size; i++) {
				ContentValues value = new ContentValues();
				DishCategory dishCategory = listDishCategory.get(i);
				value.put(DISH_CATEGORY_ID, dishCategory.getDishCategoryId());
				value.put(DISH_CATEGORY_NAME, dishCategory.getDishCategoryName());
				value.put(DISH_CATEGORY_IMAGE, dishCategory.getDishCategoryImage());
				
				allValues[i] = value;
			}
			ContentResolver resolver = RestaurantApp.CURRENT_CONTEXT.getContentResolver();
			int rowadd = resolver.bulkInsert(RestaurantAppContentProvider.DISH_CATEGORY_CONTENT_URI, allValues);
			return rowadd;
			//clode progress
		}
		return 0;
	}
	
	public static ArrayList<DishCategory> getAllDishCategory()
	{
		ContentResolver resolver = RestaurantApp.CURRENT_CONTEXT.getContentResolver();
		ArrayList<DishCategory> arrayList = new ArrayList<DishCategory>();
		Cursor cursor = resolver.query(RestaurantAppContentProvider.DISH_CATEGORY_CONTENT_URI, null, null, null, DISH_CATEGORY_NAME + " ASC");
		if(cursor != null){
			//cach 1
			/*if(cursor.moveToFirst()) {
				do{
					//TODO
				} while(cursor.moveToNext());
			}*/
			//cach 2
			if(cursor.getCount()>0){
				try {
					while (!cursor.isLast()) {
						cursor.moveToNext();
						DishCategory dishCategory = new DishCategory();
						
						String dishCategoryId = cursor.getString(cursor.getColumnIndex(DISH_CATEGORY_ID));
						dishCategory.setDishCategoryId(dishCategoryId);
						
						String dishCategoryName = cursor.getString(cursor.getColumnIndex(DISH_CATEGORY_NAME));
						dishCategory.setDishCategoryName(dishCategoryName);
						
						String dishCategoryImage = cursor.getString(cursor.getColumnIndex(DISH_CATEGORY_IMAGE));
						dishCategory.setDishCategoryImage(dishCategoryImage);
						
						arrayList.add(dishCategory);
					}
				} catch (Exception e) {
					// TODO: handle exception
					Log.e(TAG, "ERROR getAllDishCategory()", e);
				}
			}
			cursor.close();
		}
		return arrayList;
	}
}
